package com.songcha.sakura.mvvm.comic.episode

import android.os.*
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.sakura.R
import com.songcha.sakura.bean.ComicM3u8Bean
import com.songcha.sakura.databinding.FragmentComicEpisodeBinding
import com.songcha.sakura.download.DownloadServiceController
import com.songcha.sakura.ui.adapter.ComicEpisodeAdapter
import com.songcha.sakura.ui.decoration.ComicEpisodeItemDecoration
import com.songcha.sakura.struct.EpisodeListBundleData
import com.songcha.sakura.util.MyJumpUtil
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.sakura.ui.base.BaseComicMvvmFragment

class ComicEpisodeFragment  : BaseComicMvvmFragment<ComicEpisodeViewModel, FragmentComicEpisodeBinding>(){
    companion object{
        val TAG:String=ComicEpisodeFragment::class.java.simpleName
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_comic_episode
    }

    override fun isNeedShowLoadingDialog(): Boolean {
        return true
    }

    private fun toVideo(bean: ComicM3u8Bean){
        MyJumpUtil.toVideo(requireContext(),bean.m3u8Url,bean.title)
    }


    override fun initView(savedInstanceState: Bundle?){
        super.initView(savedInstanceState)
        arguments?.let { it ->
            viewModel.thumb=it.getString("thumb")?:""

            it.getSerializable("episode_list")?.let {list->
                viewModel.episodeList=(list as EpisodeListBundleData).data
            }

        }

        databinding.rcvEpisode.apply {
            layoutManager=GridLayoutManager(context,4)
            addItemDecoration(ComicEpisodeItemDecoration())
            adapter=ComicEpisodeAdapter(context, R.layout.item_episode,viewModel.episodeList)

            (adapter as ComicEpisodeAdapter).apply {
                setOnItemClickListener{
                        a,v,p->
                    viewModel.getM3u8(viewModel.episodeList[p].href,false)

                }
                setOnItemLongClickListener{a,v,p->
                    //viewModel.showDownloadDialog(requireContext(),mEpisodeList[p].episode, mEpisodeList[p].href)
                    showLoadingDialog(true)
                    viewModel.getM3u8(viewModel.episodeList[p].href,true)
                    true
                }
            }

        }


        if(viewModel.isInit) return

        viewModel.downloadM3u8Url.observe(viewLifecycleOwner,object :Observer<ComicM3u8Bean?>{
            override fun onChanged(t: ComicM3u8Bean?) {
                if(t==null) return
                hideLoadingDialog()
                showDownloadDialog(t!!)
            } }
        )


        viewModel.playM3u8Url.observe(viewLifecycleOwner,object :Observer<ComicM3u8Bean?>{
            override fun onChanged(t: ComicM3u8Bean?) {
                if(t==null) return
                toVideo(t!!)
            } }
        )
/*

        viewModel.comicM3u8Url.observe(viewLifecycleOwner){
            //if(!viewModel.isOper) return@observe
           // viewModel.isOper=false

            if(viewModel.isDownload){
                hideLoadingDialog()
                showDownloadDialog(it)
            }else{
                toVideo(it)
            }

        }*/


    }


    /*fun showLoadingDialog(){
        mLoadingDialog=LoadingDialog(requireContext())
        mLoadingDialog!!.show()
    }

    fun hideLoadingDialog(){
        mLoadingDialog?.dismiss()
    }*/

    fun showDownloadDialog(bean: ComicM3u8Bean){
        val db= CustomDialog.Companion.Builder(requireContext())
            .setDialogTitle("下载提示")
            .setDialogMsg("${bean.title}")
            .setOnConfirmListener {
                val title=bean.title
                val url=bean.m3u8Url

                val uuid=(System.currentTimeMillis()/ 1000).toString()
                DownloadServiceController.createTask(url,viewModel.thumb,uuid+"-"+title)
                true
            }

        db.create().show()
    }

    override fun onLoadError(e: Throwable) {
        super.onLoadError(e)
    }

    override fun onDestroyView() {
        super.onDestroyView()
       // _binding = null
    }
}